<?php
/**
 * @author Anh Nguyen
 * @copyright 2012
 */

defined('APP') or die('Denied access !');

class Template
{
    public    $tpl_dir = '';
    public    $tpl_ext = '';
    protected $block_main = 'main';
    protected $block_start_delim = '<!-- ';
    protected $block_end_delim = ' -->';
    protected $block_start_word = 'BEGIN: ';
    protected $block_end_word = 'END: ';
    protected $block_start_box = 'Bbox: ';
    protected $block_end_box = 'Ebox: ';
    protected $block_file_load = 'FILE: ';
    protected $tag_start_delim = '{';
    protected $tag_end_delim = '}';
    protected $tpl_content = '';
    protected $tpl_blocks = array();
    protected $tpl_tags = array();
    protected $parsed_blocks = array();
    protected $file_start_exists = false;
    
    public function tpl_start($file)
    {
        $file = $file . '.' . $this->tpl_ext;
        if( !file_exists($this->tpl_dir . DS . $file)) {
            $this->tpl_content = 'Cannot find template file : '.$this->tpl_dir.'/'.$file;
        } else {
            $this->tpl_content = file_get_contents($this->tpl_dir . DS . $file);
            $this->setup_files();
            $this->auto_get_blocks();
        }
    }
    
    protected function setup_files() {
        preg_match_all('|'.$this->block_start_delim.$this->block_file_load.'([A-Za-z0-9\._-]+?)'.$this->block_end_delim.'|U',$this->tpl_content,$file_content);
        $total_file = count($file_content[0]);
        for ($i = 0; $i < $total_file; $i ++) {
            file_exists($this->tpl_dir.DS.$file_content[1][$i])?$content = @file_get_contents($this->tpl_dir.DS.$file_content[1][$i]):$content = 'Không tìm thấy File: '.$this->tpl_dir.DS.$file_content[1][$i];
            $this->tpl_content = str_replace($file_content[0][$i],$content,$this->tpl_content);
        }
    }
    
    protected function auto_get_blocks() {
        preg_match_all('|'.$this->block_start_delim.$this->block_start_word.'([A-Za-z0-9\._-]+?)'.$this->block_end_delim.'|U',$this->tpl_content,$blocks);
        foreach ($blocks[1] as $content) {
            preg_match('/'.$this->block_start_delim.$this->block_start_word.$content.$this->block_end_delim.'(.{0,})'.$this->block_start_delim.$this->block_end_word.$content.$this->block_end_delim.'/is',$this->tpl_content,$blocks_content);
            $this->tpl_blocks[$content] = $blocks_content[1];
        }
    }
    
	public function assign($name, $value = '') {
		if (is_array($name)) {
			foreach ($name as $key => $val) {
				$this->tpl_tags[$key] = $val;
			}
		} 
        else {
			$this->tpl_tags[$name] = $value;
		}
	}
    
    public function parse($block_name) {
        if (isset($this->tpl_blocks[$block_name])) {
            $copy = $this->tpl_blocks[$block_name];
            preg_match_all('|'.$this->block_start_delim.$this->block_start_word.'([A-Za-z0-9\._-]+?)'.$this->block_end_delim.'|U',$copy, $block_array);
            $block_array = $block_array[1];
            foreach ($block_array as $block) {
                if (isset($this->parsed_blocks[$block])) $copy = str_replace($this->block_start_delim.$this->block_start_word.$block.$this->block_end_delim.$this->tpl_blocks[$block].$this->block_start_delim.$this->block_end_word.$block.$this->block_end_delim, $this->parsed_blocks[$block], $copy);
                else $copy = str_replace($this->block_start_delim.$this->block_start_word.$block.$this->block_end_delim.$this->tpl_blocks[$block].$this->block_start_delim.$this->block_end_word.$block.$this->block_end_delim, '', $copy);
                $this->parsed_blocks[$block] = null;
            }
            preg_match_all('|'.$this->tag_start_delim.'([A-Za-z0-9\._-]+?)'.$this->tag_end_delim.'|U',$copy,$tag_array);
            $tag_array = $tag_array[1];
            foreach ($tag_array as $tag) {
                if (isset($this->tpl_tags[$tag]))$copy = str_replace($this->tag_start_delim.$tag.$this->tag_end_delim,$this->tpl_tags[$tag],$copy);
                $this->tpl_tags[$tag] = null;
            }
            if (isset($this->parsed_blocks[$block_name])) $this->parsed_blocks[$block_name] .= $copy;
            else $this->parsed_blocks[$block_name] = $copy;
        }
    }
    
    public function tpl_out() {
        if ($this->file_start_exists == false) {
            $this->parse($this->block_main);
            $this->tpl_content = $this->parsed_blocks[$this->block_main];
        }
        echo $this->tpl_content;
    }    
}
?>